package org.arch_learn.spring_cloud.scn_homework.service_email_8082.endpoint;

import org.arch_learn.spring_cloud.scn_homework.service_common_dependency.request.SendEmailRequest;
import org.arch_learn.spring_cloud.scn_homework.service_email_8082.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailEndpoint {
    @Autowired
    private IEmailService iEmailService;

    @PostMapping(value = "/email/{email}/{code}")
    public ResponseEntity<Boolean> sendEmail(@PathVariable String email,
                                             @PathVariable String code) {
        ResponseEntity responseEntity = null;
        try {
            SendEmailRequest sendEmailRequest = new SendEmailRequest();
            sendEmailRequest.setSubject("验证码");
            sendEmailRequest.setContent(code);
            sendEmailRequest.setDestination(email);
            iEmailService.sendEmail(sendEmailRequest);
            responseEntity = ResponseEntity.ok(true);
        } catch (Exception e) {
            responseEntity = ResponseEntity.badRequest().body(false);
        }
        return responseEntity;
    }
}
